from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils import timezone

# 角色选择
ROLE_CHOICES = (
    ('student', '学生'),
    ('teacher', '教师'),
    ('admin', '管理员'),
)

class User(AbstractUser):
    role = models.CharField(max_length=10, choices=ROLE_CHOICES, default='student', verbose_name='角色')
    username = models.CharField(max_length=30, unique=True, verbose_name='用户名')
    password = models.CharField(max_length=128, verbose_name='密码')
    email = models.EmailField(unique=True, verbose_name='邮箱')
    phone = models.CharField(max_length=20, blank=True, null=True,  verbose_name='手机号')
    is_active = models.BooleanField(default=True, verbose_name='是否激活')
    created_at = models.DateTimeField(default=timezone.now,  verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    def is_student(self):
        return self.role == 'student'

    def is_teacher(self):
        return self.role == 'teacher'

    def is_admin(self):
        return self.role == 'admin'

    def get_role_label(self):
        return dict(ROLE_CHOICES).get(self.role, '未知角色')

    class Meta:
        verbose_name = '用户'
        verbose_name_plural = '用户'


class Student(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True, related_name='student_profile')
    student_id = models.CharField(max_length=20, unique=True, verbose_name='学号')
    major = models.CharField(max_length=100, verbose_name='专业')
    grade = models.IntegerField()
    class_name = models.CharField(max_length=50, verbose_name='班级')

    def __str__(self):
        return f"{self.user.username} - 学生"

class Teacher(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True, related_name='teacher_profile')
    teacher_id = models.CharField(max_length=20, unique=True, verbose_name='工号')
    title = models.CharField(max_length=50, verbose_name='职称')  # 职称
    department = models.CharField(max_length=100, verbose_name='所属院系')  # 所属院系

    def __str__(self):
        return f"{self.user.username} - 教师"

class Administrator(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True, related_name='admin_profile')
    admin_id = models.CharField(max_length=20, unique=True, verbose_name='管理员编号')
    department = models.CharField(max_length=100, verbose_name='所属部门') # 所属部门

    def __str__(self):
        return f"{self.user.username} - 管理员"

class Topic(models.Model):
    name = models.CharField(max_length=255, verbose_name="课题名称")
    description = models.TextField(verbose_name="课题描述")
    teacher = models.ForeignKey('Teacher', on_delete=models.CASCADE, verbose_name="指导教师")
    student = models.ForeignKey('Student', on_delete=models.SET_NULL, null=True, blank=True, verbose_name="选课学生")
    max_students = models.IntegerField(default=1, verbose_name="最大选题人数")
    current_students = models.IntegerField(default=0, verbose_name="当前选题人数")
    status = models.CharField(
        max_length=10,
        choices=[
            ('available', '可用'),
            ('pending', '待审核'),
            ('approved', '已批准'),
            ('rejected', '已驳回')
        ],
        default='available',
        verbose_name="状态"
    )

    def __str__(self):
        return self.name

class Meta:
       verbose_name = '课题'
       verbose_name_plural = '课题'
class Progress(models.Model):
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE, related_name='progresses')
    stage = models.CharField(max_length=20, choices=[('opening', '开题'), ('midterm', '中期'), ('final', '终稿')])
    content = models.TextField(verbose_name='进度内容')
    attachment = models.FileField(upload_to='progress_attachments/', blank=True, null=True, verbose_name='附件')
    submitted_at = models.DateTimeField(auto_now_add=True, verbose_name='提交时间')
    score = models.IntegerField(blank=True, null=True, verbose_name='评分')
    comment = models.TextField(blank=True, null=True, verbose_name='评语')
    status = models.CharField(max_length=20, choices=[('passed', '通过'), ('rejected', '打回')], blank=True, null=True,verbose_name='状态')
    def __str__(self):
        return f"{self.topic.name} - {self.stage}"

    class Meta:
        verbose_name = '进度'
        verbose_name_plural = '进度'
